package cn.zhejianglao.shirotemplate.service.permission;

import cn.zhejianglao.shirotemplate.common.model.PagedResult;
import cn.zhejianglao.shirotemplate.common.model.PermissionVO;
import cn.zhejianglao.shirotemplate.common.model.Response;
import cn.zhejianglao.shirotemplate.common.validator.ParamValidator;
import cn.zhejianglao.shirotemplate.service.permission.model.RolePermissionVO;
import cn.zhejianglao.shirotemplate.service.permission.service.IPermissionService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequiresRoles("admin")
    @GetMapping("/list/{size}/{page}")
    public Response<PagedResult<PermissionVO>> list(@PathVariable("size") int size, @PathVariable("page") int page) {
        return Response.success(permissionService.list(size, page));
    }

    @RequiresRoles("admin")
    @PostMapping("/create")
    public Response<String> create(@RequestBody PermissionVO permissionVO) {
        permissionService.create(permissionVO);
        return Response.success();
    }

    @RequiresRoles("admin")
    @DeleteMapping("delete/{id}")
    public Response<String> delete(@PathVariable("id") Long id) {
        permissionService.delete(id);
        return Response.success();
    }

    @RequiresRoles("admin")
    @PostMapping("/update")
    public Response<String> update(@RequestBody PermissionVO permissionVO) {
        permissionService.update(permissionVO);
        return Response.success();
    }

    @RequiresRoles("admin")
    @PostMapping("/authorize")
    public Response<String> authorize(@RequestBody RolePermissionVO relation) {
        ParamValidator.validate(relation);
        permissionService.authorize(relation);
        return Response.success();
    }

    @RequiresRoles("admin")
    @PostMapping("/cancelAuthorization")
    public Response<String> cancelAuthorization(@RequestBody RolePermissionVO relation) {
        ParamValidator.validate(relation);
        permissionService.cancelAuthorization(relation);
        return Response.success();
    }
}
